package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
@Api(tags = "/菜品管理")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;
    /**
     * 新增菜品
     * @return
     */
    @PostMapping("/dish")
    @ApiOperation("新增菜品")
    public Result addDish(@RequestBody DishDTO dishDTO){
        log.info("新增菜品:{}",dishDTO);
        dishService.saveDishWithFlavor(dishDTO);
        return Result.success("操作成功");
    }

    /**
     * 菜品修改
     * @param dishDTO
     * @return
     */
    @PutMapping("/dish")
    @ApiOperation("菜品修改")
    public Result updateDish(@RequestBody DishDTO dishDTO){
        log.info("菜品修改:{}",dishDTO);
        dishService.update(dishDTO);
        return Result.success("操作成功！");
    }

    /**
     * 根据id获取菜品信息
     * @param id
     * @return
     */
    @GetMapping("/dish/{id}")
    @ApiOperation("根据ID获取菜品信息")
    public Result<DishVO> getDish(@PathVariable Long id){
        log.info("当前查询ID（菜品）{}",id);
        return Result.success(dishService.getDishInfoById(id));
    }
    /**
     * 菜品信息分页查询
     * @param
     * @return
     */
    @GetMapping("/dish/page")
    @ApiOperation("菜品信息分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品信息分页查询{}",dishPageQueryDTO);
        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }
    @DeleteMapping("/dish")
    @ApiOperation("删除菜品信息")
    public Result delete(String ids){
        String[] idList = ids.split(",");
        dishService.bachDelete(idList);
        return Result.success("删除成功!");
    }

    /**
     * 根据分类Id查询菜品集合
     * @return
     */
    @GetMapping("/dish/list")
    @ApiOperation("根据分类Id查询菜品")
    @Cacheable(cacheNames = "dish",key = "#categoryId")
    public Result<List<Dish>> getDishListByCategoryId(Long categoryId){
        List<Dish> dishs = dishService.getDishByCategoryId(categoryId);
        return Result.success(dishs);
    }

}
